package com.qidian.server.workerThread;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.qidian.common.Globals;
import com.qidian.common.beans.AccountBean;
import com.qidian.connection.util.ConnectionPool;

public final class Synchronizer_Account extends WorkerThread
{
	private static Logger logger = Logger.getLogger(Synchronizer_Account.class) ;
	private static Synchronizer_Account accountSynchronizer = new Synchronizer_Account() ;
	
//    int interval = 30*1000 ;
    int batch_size = 100 ;
    protected Thread       runningThread= null;
    
    private Synchronizer_Account()
    {
    	
    }

    public void run()
    {
        synchronized(this){
            this.runningThread = Thread.currentThread();
        }
        AccountBean account = new AccountBean() ;
        int loopTimes = 0 ;
        try
        {
            ConcurrentHashMap<String,AccountBean> accountBuf = Globals.getAccountBuffer().buffer;
            while(keepRunning)
            {
            	loopTimes ++ ;
                Connection conn = null ;
                PreparedStatement ps = null ;
                try
                {
                	
                    conn = ConnectionPool.getConnection() ;
                    ps = account.getPreparedStatement(conn) ;
                    int i = 0 ;
                    Iterator<AccountBean> accountItr = accountBuf.values().iterator() ;
                    while(accountItr.hasNext())
                    {
                        account = accountItr.next() ;
//                        if (account.needSynchronized() || (loopTimes % batch_size == (batch_size-1)))
                        if (account.needSynchronized())
                        {
                        	// 如果报错过于频繁
                        	if (account.getReportCountIncrement() > 10)
                        	{
                        		logger.error("报错过于频繁:"+account.getAccount()) ;
                        	}
                        	
                            if(!account.setParameters(ps))
                                continue;
                            ps.addBatch() ;
                            i++ ;
                            if (i%batch_size == (batch_size-1))
                            {
                                ps.executeBatch() ;
                                ps.clearBatch() ;
                            }
                            if (account.isDeleted())
                            	accountBuf.remove(account.getAccount()) ;
                        }

                    }
                    
                    ps.executeBatch() ;
                    ps.clearBatch() ;
                    ps.close() ;
                    
                     //清理子账号表 删除子账号,2
                    if(loopTimes%2 == 0)
                    {
                    	Statement stt = conn.createStatement() ;
                    	stt.executeUpdate(AccountBean.clearSql) ;
                    	stt.close() ;
                    }
                    
                }
                catch (SQLException e)
                {
                    e.printStackTrace() ;
                    logger.error(e.getMessage()) ;
                }
                finally
                {
                	ConnectionPool.freeConn(conn) ;
                }
                
                Thread.sleep(interval) ;
            }
        }
        catch (Exception e)
        {
            e.getMessage() ;
            logger.error(e.getMessage()) ;
            System.exit(-1) ;
        }
    }

    @Override
    public void init()
    {
        
    }

	public static Synchronizer_Account getInstance() 
	{
		return accountSynchronizer;
	}
}
